using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using elibrary.Classes;
using elibrary.Models;
using elibrary.Models.Entities;

namespace elibrary.Models.Managers
{
	public class FeedbackManager
	{
		private DataContext ctx;
		private DbSet<Feedback> feedbacks;

		public FeedbackManager(DataContext ctx)
		{
			this.ctx = ctx;
			this.feedbacks = ctx.feedbacks;
		}

        public void add(Feedback feedback, bool autoSave = true)
        {
            feedbacks.Add(feedback);

            if (autoSave)
                ctx.SaveChanges();
        }

        public Feedback get(int id)
        {
            return feedbacks.SingleOrDefault(q => q.id == id);
        }

        public Feedback postFeedback(string n, string e, string msg)
        {
            Feedback feedback = new Feedback{ dateAdded = DateTime.Now, name = n, 
                email = e, message = msg}

            add(feedback);

            return feedback;
        }

        public void delete(int id, bool autoSave = true)
        {
            delete(get(id), autoSave);
        }

        public void delete(Feedback feedback, bool autoSave = true)
        {
            if (feedback == null)
                return;

            feedbacks.Remove(feedback);

            if (autoSave)
                ctx.SaveChanges();
        }
	}
}